package com.hp.attendance.dao;

import com.hp.attendance.entity.Department;
import com.hp.attendance.util.BaseDAO;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class DepartmentDao extends BaseDAO {
    public List<Department> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from department";
        List<Department> list = queryList(sql, new Object[0], new Department());
        return list;
    }
   //分页查询记录
    public  List<Department> getLimitList(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from department limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<Department> departments = queryList(sql, obj, new Department());
        return departments;
    }

    //根据id查询对象
    public Department getDepartment(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from department where id=?";
        Object[] obj = {id};
        Department department = queryOne(sql, obj, new Department());
        return department;
    }
    //查询总记录
    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from department";
        int count = queryCount(sql, new Object[0]);
        return count;
    }
    /**
     * 添加部门
     * @param department
     */
    public void addDepartment(Department department) throws SQLException, IOException, ClassNotFoundException {
        String sql ="insert into department values(null,?,?,null,?,?,?)";
        Object[] obj={department.getDepartment_number(),department.getName(),department.getTelephone(),department.getAddress(),department.getNotes()};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 修改菜单
     * @param
     */
    public void updateDepartment(Department department) throws SQLException, IOException, ClassNotFoundException {
        String sql ="update department set department_number=?, name=?,manager=null,telephone=?,address=?,notes=? where id=?";
        Object[] obj={department.getDepartment_number(),department.getName(),department.getTelephone(),department.getAddress(),department.getNotes(),department.getId()};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 删除菜单
     * @param id
     */
    public void delDepartment(int id) throws SQLException, IOException, ClassNotFoundException {
        String sql ="delete from department where id=?";
        Object[] obj ={id};
        addOrUpdOrDel(sql,obj);
    }

//部门编号最大值的查询
public Integer getMaxDepartmentnumber() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    String sql ="select MAX(department_number) from department";
    int i = queryCount(sql, new Object[0]);
    return i;
}





  }

